import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login'
import Layout from '@/views/layout'
import NoteFound from '@/views/404'
import Home from '@/views/home'
import User from '@/views/user'
import Queastion from '@/views/question'
import Video from '@/views/video'
import store from '@/store/index.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '/home',
        component: Home
      },
      {
        path: '/video',
        component: Video
      },
      {
        path: '/queastion',
        component: Queastion
      }, {
        path: '/user',
        component: User
      }
    ]
  },
  {
    meta: [
      { name: '登录' }
    ],
    path: '/login',
    component: Login
  },
  {
    path: '*',
    component: NoteFound
  }
]

const router = new VueRouter({
  routes
})
const whiteList = ['/home', '/', '/video', '/queastion']
router.beforeEach(function (to, from, next) {
  const token = store.state.user.tokenInfo.token
  if (token) {
    return next()
  }
  if (whiteList.includes(to.path)) {
    return next()
  }
  next('/login')
})

export default router
